﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 玩具商城管理系统.Order
{
    public partial class Check_order : Form
    {
        public Check_order()
        {
            InitializeComponent();
        }
        SqlDataAdapter daOrder;
        DataSet ds =new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from orderT";
            daOrder = new SqlDataAdapter(str, DB.cn);
            daOrder.Fill(ds, "order_info");
            
        }
        void showAll()
        {
            init();
            showXz();
        }
        void showXz()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvorder.Columns.Add(acCode);   


        }

        private void Check_order_Load(object sender, EventArgs e)
        {
            if(login.Dflag==false)
            {
                MessageBox.Show("需要以销售部员工身份登录才可以进入");
                this.Close();
            }
            else
            {
                showAll();
                DataView dvOrder = new DataView(ds.Tables["order_info"]);
                dgvorder.DataSource = dvOrder;
                this.dgvorder.Columns[1].HeaderText = "订单编号";
                this.dgvorder.Columns[2].HeaderText = "用户名称";
                this.dgvorder.Columns[3].HeaderText = "下单时间";
                this.dgvorder.Columns[4].HeaderText = "用户地址";
                this.dgvorder.Columns[5].HeaderText = "用户省份";
                this.dgvorder.Columns[6].HeaderText = "用户城市";
                this.dgvorder.Columns[7].HeaderText = "用户地区";
                this.dgvorder.Columns[8].HeaderText = "地区邮编";
                this.dgvorder.Columns[9].HeaderText = "审核状态";
                this.dgvorder.Columns[10].HeaderText = "审核人员";
                this.dgvorder.Columns[11].HeaderText = "订单收益";
                this.dgvorder.Columns[12].HeaderText = "用户备注";
            }
        }

        private void dgvorder_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvorder.Rows.Count > 0)
            {
                for (int i = 0; i < dgvorder.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dgvorder.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }

            }
        }

        private void btnok_Click(object sender, EventArgs e)
        {
            int s=dgvorder.Rows.Count;
            for (int i = 0;i < dgvorder.Rows.Count;i++)
            {//更新客户订单表hot
                if (dgvorder.Rows[i].Cells["acCode"].EditedFormattedValue.ToString()=="True")
                {
                    dgvorder.Rows[i].Cells["orderT_check"].Value = "已审核";
                    DB.GetCn();
                    string result = dgvorder.Rows[i].Cells["orderT_id"].Value.ToString();
                    string pro = "";
                    pro = "update hot set hot_status='已审核',hot_people='已完成'"+" where orderT_id = '"+result+"'";
                    DB.sqlEx(pro);

                    //获取顾客积分
                    DB.GetCn();
                    string result2 = dgvorder.Rows[i].Cells["orderT_cid"].Value.ToString();
                    string power = "select client_power from client where client_name='" + result2 + "'";
                    SqlCommand cmd = new SqlCommand(power, DB.cn);
                    SqlDataReader rdr2 = cmd.ExecuteReader();
                    while (rdr2.Read())
                    {
                        power = rdr2["client_power"].ToString();
                    }
                    rdr2.Close();
                    DB.GetCn();
                    string qq = "";
                     if (power == "高级用户")
                        {
                            qq = " UPDATE client SET client_count = 10 + 0.8*(SELECT SUM(commodity_money * hot_commoditynum) AS '商品总价'    FROM hot    JOIN commodity ON commodity_name = hot_commodityname    WHERE hot_clientname = '" + result2 + "'    AND hot_status = '已审核') WHERE client_name = '" + result2 + "';";

                        }
                     else if (power == "初级用户")
                        {
                            qq = " UPDATE client SET client_count = 10 + 0.9*(SELECT SUM(commodity_money * hot_commoditynum) AS '商品总价'    FROM hot    JOIN commodity ON commodity_name = hot_commodityname    WHERE hot_clientname = '" + result2 + "'    AND hot_status = '已审核') WHERE client_name = '" + result2 + "';";

                        }
                     else
                        {
                            qq = " UPDATE client SET client_count = 10 + (SELECT SUM(commodity_money * hot_commoditynum) AS '商品总价'    FROM hot    JOIN commodity ON commodity_name = hot_commodityname    WHERE hot_clientname = '" + result2 + "'    AND hot_status = '已审核') WHERE client_name = '" + result2 + "';";

                        }
                        DB.sqlEx(qq);
                    //更新用户等级
                    DB.GetCn();
                    string result3 = dgvorder.Rows[i].Cells["orderT_cid"].Value.ToString();
                    string count = "select * from client where client_name='" + result3 + "'";
                    SqlCommand cmd3 = new SqlCommand(count, DB.cn);
                    SqlDataReader rdr3 = cmd3.ExecuteReader();
                    while (rdr3.Read())
                    {
                        count = rdr3["client_count"].ToString();
                        
                    }
                    rdr3.Close();
                    DB.GetCn();
                    decimal countValue = decimal.Parse(count);
                    string changepower = "";
                    if (countValue < 200)
                    {
                        changepower = "update client set client_power = '普通用户' where client_name='" + result2 + "'";
                    }
                    else if (countValue >= 200 && countValue < 400)
                    {
                        changepower = "update client set client_power='初级用户' where client_name='" + result2 + "'";
                    }
                    else
                    {
                        changepower = "update client set client_power='高级用户' where client_name='" + result2 + "'";
                    }
                    DB.sqlEx(changepower);
                    if (string.IsNullOrEmpty(dgvorder.Rows[i].Cells["employee_id"].Value?.ToString()) )
                    {
                        dgvorder.Rows[i].Cells["employee_id"].Value = login.username;
                    }
                    UpdateDB();
                    MessageBox.Show("审核成功");
                }
                else
                {
                    s = s - 1;
                }
            }
            if (s == 0)
            {
                MessageBox.Show("请选择你要审核的订单");
            }
        }
        void UpdateDB()//更新数据源
        {
            try
            {
                SqlCommandBuilder dbOrder = new SqlCommandBuilder(daOrder);
                daOrder.Update(ds, "Order_info");
            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
